{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "478889ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "使用os模块操作文件以及目录路径，定义一个统计指定文件夹大小的函数，（返回文件夹大小【非占用空间】）单位：字节(B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3bd28101",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "499357b6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "文件夹大小 21871552027\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "#定义统计目录大小函数\n",
    "def dirsize(dir):\n",
    "    sum=0\n",
    "#判断目录是文件或目录\n",
    "    if os.path.isfile(dir):\n",
    "        sum+=os.path.getsize(dir)\n",
    "    if os.path.isdir(dir):\n",
    "        ldir=os.listdir(dir)\n",
    "        for i in ldir:\n",
    "            file=os.path.join(dir,i)\n",
    "            if os.path.isdir(file):\n",
    "                sum+=dirsize(file)   #递归调用统计目录大小函数\n",
    "            if os.path.isfile(file):\n",
    "                sum+=os.path.getsize(file)\n",
    "    return sum\n",
    "print('文件夹大小',dirsize(\"D:\\\\百度网盘\")) #输入需要统计大小的目录或文件地址"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6caf3c8a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
